package TP;

import fr.inria.acacia.corese.exceptions.EngineException;
import fr.inria.edelweiss.kgram.core.Mappings;
import fr.inria.edelweiss.kgraph.core.Graph;
import fr.inria.edelweiss.kgraph.query.QueryProcess;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;

/**
 *
 * @author userepu
 */
@WebServlet(name = "addGenre", urlPatterns = {"/addGenre"})
public class addGenre extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, EngineException, URISyntaxException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {

//        String dbpedia_service = "<http://dbpedia.org/sparql>";
            ServletContext context = getServletConfig().getServletContext();
            Graph graph = Server.getGraph(context);
            String artiste = request.getParameter("artiste");
            String newGenre = request.getParameter("genre");
            QueryProcess exec = QueryProcess.create(graph);

            String query = " prefix db-owl: <http://dbpedia.org/ontology/>\n"
                    + "\n"
                    + "INSERT \n"
                    + "{\n"
                    + "    <" + artiste + "> db-owl:genre ?genre."
                    + "} WHERE {"
                    + "    ?genre rdfs:label ?label."
                    + "    filter regex( ?label, \"" + newGenre + "\", \"i\")\n"
                    + "}";
            String queryKurt = "prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>\n"
                    + " prefix db-owl: <http://dbpedia.org/ontology/>\n"
                    + "\n"
                    + "SELECT  ?genre  WHERE\n"
                    + "{\n"
                    + "    <http://fr.dbpedia.org/resource/Kurt_Cobain> db-owl:genre ?genre.\n"
                    + "}";

            Logger.getLogger(addGenre.class.getName()).log(Level.WARNING, "la requete " + query);
            JSONArray jsonarray = new JSONArray();
            Mappings map = exec.query(query);
            QueryProcess exec2 = QueryProcess.create(graph);
            Mappings map2 = exec2.query(queryKurt);
            //Map<String, String> genres = new HashMap();

            Logger.getLogger(addGenre.class.getName()).log(Level.WARNING, "la kurt " + map2.toString());
            //request.setAttribute("genres", genres);

            request.getRequestDispatcher("artiste.jsp").forward(request, response);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (EngineException ex) {
            Logger.getLogger(addGenre.class.getName()).log(Level.SEVERE, null, ex);
        } catch (URISyntaxException ex) {
            Logger.getLogger(addGenre.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
